<?php

namespace Hdphim\Modules\Admin\Controllers;

use Phalcon\Mvc\Controller as Controller;
use Hdphim\Modules\Admin\Forms\User\FormCreateUser as FormCreateUser;
use Hdphim\Modules\Admin\Models\Users\Users as AdminUsers;
use Hdphim\Modules\Admin\Models\Users\UsersProfile as UsersProfile;

class UserController extends Controller
{
    public function indexAction()
    {
        $this->view->users = AdminUsers::find();
    }

    public function createAction()
    {
    	$form = new FormCreateUser();
                
    	if($this->request->isPost())
    	{
    		if($form->isValid($this->request->getPost()) != false)
    		{
                $user = new AdminUsers();

                $user->assign(array(
                    'name'          => $this->request->getPost('name') ,
                    'email'         => $this->request->getPost('email'),
                    'password'      => $this->security->hash($this->request->getPost('password')),
                    'group_id'      => 5,
                    'banned'        => 'N',
                    'suspended'     => 'N',
                    'active'        => 'Y',
                    'created_at'    => date("Y-m-d H:i:s"),
                    'updated_at'    => date("Y-m-d H:i:s")
                ));

                if ($user->save()) {

                    $userProfiles = new UsersProfile();
                    $userProfiles->assign(array(
                        'users_id' => $user->getId(),
                        'created_at'    => date("Y-m-d H:i:s"),
                        'updated_at'    => date("Y-m-d H:i:s")
                    ));

                    $userProfiles->save();

                    return $this->dispatcher->forward(array(
                        'controller' => 'user',
                        'action' => 'index'
                    ));
                }

                $this->flash->error($user->getMessages());

    		}
    	}

    	$this->view->form = $form;
    }
}
